Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports System.Threading
Imports Microsoft.Win32

''' <summary> 
''' Represents a most recently used (MRU) menu. 
''' </summary> 
''' <remarks>This class shows the MRU list in a popup menu. To display 
''' the MRU list "inline" use <see name="MruMenuInline" />. 
''' <para>The class will optionally load the last set of files from the registry 
''' on construction and store them when instructed by the main program.</para> 
''' <para>Internally, this class uses zero-based numbering for the items. 
''' The displayed numbers, however, will start with one.</para></remarks> 
Public Class clsMruStripMenu
    Private m_clickedHandler As ClickedHandler
    Protected recentFileMenuItem As ToolStripMenuItem
    Protected m_registryKeyName As String
    Protected m_numEntries As Integer = 0
    Protected m_maxEntries As Integer = 4
    Protected m_maxShortenPathLength As Integer = 96
    Protected mruStripMutex As Mutex

#Region "MruMenuItem"

    ''' <summary> 
    ''' The menu item which will contain the MRU entry. 
    ''' </summary> 
    ''' <remarks>The menu may display a shortened or otherwise invalid pathname. 
    ''' This class stores the actual filename, preferably as a fully 
    ''' resolved labelName, that will be returned in the event handler.</remarks> 
    Public Class MruMenuItem
        Inherits ToolStripMenuItem
        ''' <summary> 
        ''' Initializes a new instance of the MruMenuItem class. 
        ''' </summary> 
        Public Sub New()
            Tag = ""
        End Sub

        ''' <summary> 
        ''' Initializes an MruMenuItem object. 
        ''' </summary> 
        ''' <param name="filename">The string to actually return in the <paramref name="eventHandler">eventHandler</paramref>.</param> 
        ''' <param name="entryname">The string that will be displayed in the menu.</param> 
        ''' <param name="eventHandler">The EventHandler that 
        ''' handles the <see cref="MenuItem.Click">Click</see> event for this menu item.</param> 
        Public Sub New(ByVal filename As String, ByVal entryname As String, ByVal eventHandler As EventHandler)
            Tag = filename
            Text = entryname
            AddHandler Click, eventHandler
        End Sub

        ''' <summary> 
        ''' Gets the filename. 
        ''' </summary> 
        ''' <value>Gets the filename.</value> 
        Public Property Filename() As String
            Get
                Return DirectCast(Tag, String)
            End Get
            Set(ByVal value As String)
                Tag = value
            End Set
        End Property
    End Class
#End Region

#Region "Construction"

    Protected Sub New()
    End Sub

    ''' <summary> 
    ''' Initializes a new instance of the MruMenu class. 
    ''' </summary> 
    ''' <param name="recentFileMenuItem">The temporary menu item which will be replaced with the MRU list.</param> 
    ''' <param name="clickedHandler">The delegate to handle the item selection (click) event.</param> 
    Public Sub New(ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler)
        Me.New(recentFileMenuItem, clickedHandler, Nothing, False, 4)
    End Sub

    ''' <summary> 
    ''' Initializes a new instance of the MruMenu class. 
    ''' </summary> 
    ''' <param name="recentFileMenuItem">The temporary menu item which will be replaced with the MRU list.</param> 
    ''' <param name="clickedHandler">The delegate to handle the item selection (click) event.</param> 
    ''' <param name="maxEntries"></param> 
    Public Sub New(ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal maxEntries As Integer)

        Me.New(recentFileMenuItem, clickedHandler, Nothing, False, maxEntries)
    End Sub

    ''' <summary> 
    ''' Initializes a new instance of the MruMenu class. 
    ''' </summary> 
    ''' <param name="recentFileMenuItem">The temporary menu item which will be replaced with the MRU list.</param> 
    ''' <param name="clickedHandler">The delegate to handle the item selection (click) event.</param> 
    ''' <param name="registryKeyName"></param> 
    Public Sub New(ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal registryKeyName As String)
        Me.New(recentFileMenuItem, clickedHandler, registryKeyName, True, 4)
    End Sub

    ''' <summary> 
    ''' Initializes a new instance of the MruMenu class. 
    ''' </summary> 
    ''' <param name="recentFileMenuItem">The temporary menu item which will be replaced with the MRU list.</param> 
    ''' <param name="clickedHandler">The delegate to handle the item selection (click) event.</param> 
    ''' <param name="registryKeyName">The name or path of the registry key to use to store the MRU list and settings.</param> 
    ''' <param name="maxEntries">The maximum number of items on the MRU list.</param> 
    Public Sub New(ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal registryKeyName As String, ByVal maxEntries As Integer)
        Me.New(recentFileMenuItem, clickedHandler, registryKeyName, True, maxEntries)
    End Sub

    ''' <summary> 
    ''' Initializes a new instance of the MruMenu class. 
    ''' </summary> 
    ''' <param name="recentFileMenuItem">The temporary menu item which will be replaced with the MRU list.</param> 
    ''' <param name="clickedHandler">The delegate to handle the item selection (click) event.</param> 
    ''' <param name="registryKeyName">The name or path of the registry key to use to store the MRU list and settings.</param> 
    ''' <param name="loadFromRegistry">Loads the MRU settings from the registry immediately.</param> 
    Public Sub New(ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal registryKeyName As String, ByVal loadFromRegistry As Boolean)
        Me.New(recentFileMenuItem, clickedHandler, registryKeyName, loadFromRegistry, 4)
    End Sub

    ''' <summary> 
    ''' Initializes a new instance of the MruMenu class. 
    ''' </summary> 
    ''' <param name="recentFileMenuItem">The temporary menu item which will be replaced with the MRU list.</param> 
    ''' <param name="clickedHandler">The delegate to handle the item selection (click) event.</param> 
    ''' <param name="registryKeyName">The name or path of the registry key to use to store the MRU list and settings.</param> 
    ''' <param name="loadFromRegistry">Loads the MRU settings from the registry immediately.</param> 
    ''' <param name="maxEntries">The maximum number of items on the MRU list.</param> 
    Public Sub New(ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal registryKeyName As String, ByVal loadFromRegistry As Boolean, ByVal maxEntries As Integer)
        Init(recentFileMenuItem, clickedHandler, registryKeyName, loadFromRegistry, maxEntries)
    End Sub

    Protected Sub Init(ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal _registryKeyName As String, ByVal _loadFromRegistry As Boolean, ByVal _maxEntries As Integer)
        If recentFileMenuItem Is Nothing Then
            Throw New ArgumentNullException("recentFileMenuItem")
        End If

        Me.recentFileMenuItem = recentFileMenuItem
        Me.recentFileMenuItem.Checked = False
        Me.recentFileMenuItem.Enabled = False

        maxEntries = _maxEntries
        Me.m_clickedHandler = clickedHandler

        If _registryKeyName IsNot Nothing Then
            RegistryKeyName = _registryKeyName
            If _loadFromRegistry Then
                LoadFromRegistry()
            End If
        End If
    End Sub

#End Region

#Region "Event Handling"

    Public Delegate Sub ClickedHandler(ByVal number As Integer, ByVal filename As String)

    Protected Sub OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim menuItem As MruMenuItem = DirectCast(sender, MruMenuItem)
        m_clickedHandler(MenuItems.IndexOf(menuItem) - StartIndex, menuItem.Filename)
    End Sub


#End Region

#Region "Properties"

    Public Overridable ReadOnly Property MenuItems() As ToolStripItemCollection
        Get
            Return recentFileMenuItem.DropDownItems
        End Get
    End Property

    Public Overridable ReadOnly Property StartIndex() As Integer
        Get
            Return 0
        End Get
    End Property

    Public Overridable ReadOnly Property EndIndex() As Integer
        Get
            Return m_numEntries
        End Get
    End Property

    Public ReadOnly Property NumEntries() As Integer
        Get
            Return m_numEntries
        End Get
    End Property

    Public Property MaxEntries() As Integer
        Get
            Return m_maxEntries
        End Get
        Set(ByVal value As Integer)
            If value > 16 Then
                m_maxEntries = 16
            Else
                m_maxEntries = IIf(value < 4, 4, value)

                Dim index As Integer = StartIndex + m_maxEntries
                While m_numEntries > m_maxEntries
                    MenuItems.RemoveAt(index)
                    m_numEntries -= 1
                End While
            End If
        End Set
    End Property

    Public Property MaxShortenPathLength() As Integer
        Get
            Return m_maxShortenPathLength
        End Get
        Set(ByVal value As Integer)
            m_maxShortenPathLength = IIf(value < 16, 16, value)
        End Set
    End Property

    Public Overridable ReadOnly Property IsInline() As Boolean
        Get
            Return False
        End Get
    End Property

#End Region

#Region "Helper Methods"

    Protected Overridable Sub Enable()
        recentFileMenuItem.Enabled = True
    End Sub

    Protected Overridable Sub Disable()
        recentFileMenuItem.Enabled = False
        'recentFileMenuItem.MenuItems.RemoveAt(0); 
    End Sub

    Protected Overridable Sub SetFirstFile(ByVal menuItem As MruMenuItem)
    End Sub

    Public Sub SetFirstFile(ByVal number As Integer)
        If number > 0 AndAlso m_numEntries > 1 AndAlso number < m_numEntries Then
            Dim menuItem As MruMenuItem = DirectCast(MenuItems(StartIndex + number), MruMenuItem)

            MenuItems.RemoveAt(StartIndex + number)
            MenuItems.Insert(StartIndex, menuItem)

            SetFirstFile(menuItem)
            FixupPrefixes(0)
        End If
    End Sub

    Public Shared Function FixupEntryname(ByVal number As Integer, ByVal entryname As String) As String
        If number < 9 Then
            Return "&" & (number + 1) & " " & entryname
        ElseIf number = 9 Then
            Return "1&0" & " " & entryname
        Else
            Return (number + 1) & " " & entryname
        End If
    End Function

    Protected Sub FixupPrefixes(ByVal startNumber As Integer)
        If startNumber < 0 Then
            startNumber = 0
        End If

        If startNumber < m_maxEntries Then
            Dim i As Integer = StartIndex + startNumber
            While i < EndIndex
                Dim offset As Integer = IIf(MenuItems(i).Text.Substring(0, 3) = "1&0", 4, 3)
                ' MenuItems[i].Text = FixupEntryname(startNumber, MenuItems[i].Text.Substring(startNumber == 10 ? 5 : 4)); 
                MenuItems(i).Text = FixupEntryname(startNumber, MenuItems(i).Text.Substring(offset))
                i += 1
                startNumber += 1
            End While
        End If
    End Sub

    ''' <summary> 
    ''' Shortens a pathname for display purposes. 
    ''' </summary> 
    ''' <param name="pathname">The pathname to shorten.</param> 
    ''' <param name="maxLength">The maximum number of characters to be displayed.</param> 
    ''' <remarks>Shortens a pathname by either removing consecutive components of a path 
    ''' and/or by removing characters from the end of the filename and replacing 
    ''' then with three elipses (...) 
    ''' <para>In all cases, the root of the passed path will be preserved in it's entirety.</para> 
    ''' <para>If a UNC path is used or the pathname and maxLength are particularly short, 
    ''' the resulting path may be longer than maxLength.</para> 
    ''' <para>This method expects fully resolved pathnames to be passed to it. 
    ''' (Use Path.GetFullPath() to obtain this.)</para> 
    ''' </remarks> 
    ''' <returns></returns> 
    Public Shared Function ShortenPathname(ByVal pathname As String, ByVal maxLength As Integer) As String
        If pathname.Length <= maxLength Then
            Return pathname
        End If

        Dim root As String = Path.GetPathRoot(pathname)
        If root.Length > 3 Then
            root &= Path.DirectorySeparatorChar
        End If

        Dim elements As String() = pathname.Substring(root.Length).Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)

        Dim filenameIndex As Integer = elements.GetLength(0) - 1

        If elements.GetLength(0) = 1 Then
            ' pathname is just a root and filename 
            If elements(0).Length > 5 Then
                ' long enough to shorten 
                ' if path is a UNC path, root may be rather long 
                If root.Length + 6 >= maxLength Then
                    Return root & elements(0).Substring(0, 3) & "..."
                Else
                    Return pathname.Substring(0, maxLength - 3) & "..."
                End If
            End If
        ElseIf (root.Length + 4 + elements(filenameIndex).Length) > maxLength Then
            ' pathname is just a root and filename 
            root &= "...\"

            Dim len As Integer = elements(filenameIndex).Length
            If len < 6 Then
                Return root & elements(filenameIndex)
            End If

            If (root.Length + 6) >= maxLength Then
                len = 3
            Else
                len = maxLength - root.Length - 3
            End If
            Return root & elements(filenameIndex).Substring(0, len) & "..."
        ElseIf elements.GetLength(0) = 2 Then
            Return root & "...\" & elements(1)
        Else
            Dim len As Integer = 0
            Dim begin As Integer = 0
            For i As Integer = 0 To filenameIndex - 1

                If elements(i).Length > len Then
                    begin = i
                    len = elements(i).Length
                End If
            Next

            Dim totalLength As Integer = pathname.Length - len + 3
            Dim [end] As Integer = begin + 1

            While totalLength > maxLength
                If begin > 0 Then
                    totalLength -= elements(System.Threading.Interlocked.Decrement(begin)).Length - 1
                End If

                If totalLength <= maxLength Then
                    Exit While
                End If

                If [end] < filenameIndex Then
                    totalLength -= elements(System.Threading.Interlocked.Increment([end])).Length - 1
                End If

                If begin = 0 AndAlso [end] = filenameIndex Then
                    Exit While
                End If
            End While
            For i As Integer = 0 To begin - 1

                ' assemble final string 

                root &= elements(i) & "\"c
            Next

            root &= "...\"
            For i As Integer = [end] To filenameIndex - 1

                root &= elements(i) & "\"c
            Next

            Return root & elements(filenameIndex)
        End If
        Return pathname
    End Function

#End Region

#Region "Get Methods"

    ''' <summary> 
    ''' Returns the entry number matching the passed filename. 
    ''' </summary> 
    ''' <param name="filename">The filename to search for.</param> 
    ''' <returns>The entry number of the matching filename or -1 if not found.</returns> 
    Public Function FindFilenameNumber(ByVal filename As String) As Integer
        If filename Is Nothing Then
            Throw New ArgumentNullException("filename")
        End If

        If filename.Length = 0 Then
            Throw New ArgumentException("filename")
        End If

        If m_numEntries > 0 Then
            Dim number As Integer = 0
            Dim i As Integer = StartIndex
            While i < EndIndex
                If String.Compare(DirectCast(MenuItems(i), MruMenuItem).Filename, filename, True) = 0 Then Return number
                i += 1
                number += 1
            End While
        End If
        Return -1
    End Function

    ''' <summary> 
    ''' Returns the menu index of the passed filename. 
    ''' </summary> 
    ''' <param name="filename">The filename to search for.</param> 
    ''' <returns>The menu index of the matching filename or -1 if not found.</returns> 
    Public Function FindFilenameMenuIndex(ByVal filename As String) As Integer
        Dim number As Integer = FindFilenameNumber(filename)
        Return IIf(number < 0, -1, StartIndex + number)
    End Function

    ''' <summary> 
    ''' Returns the menu index for a specified MRU item number. 
    ''' </summary> 
    ''' <param name="number">The MRU item number.</param> 
    ''' <exception cref="ArgumentOutOfRangeException"></exception> 
    ''' <returns>The menu index of the passed MRU number.</returns> 
    Public Function GetMenuIndex(ByVal number As Integer) As Integer
        If number < 0 OrElse number >= m_numEntries Then
            Throw New ArgumentOutOfRangeException("number")
        End If

        Return StartIndex + number
    End Function

    Public Function GetFileAt(ByVal number As Integer) As String
        If number < 0 OrElse number >= m_numEntries Then
            Throw New ArgumentOutOfRangeException("number")
        End If

        Return DirectCast(MenuItems(StartIndex + number), MruMenuItem).Filename
    End Function

    Public Function GetFiles() As String()
        If MenuItems.Count = 0 Then Return New String() {}
        Dim filenames As String() = New String(m_numEntries - 1) {}

        Dim index As Integer = StartIndex
        Dim i As Integer = 0
        While i < filenames.GetLength(0)
            filenames(i) = DirectCast(MenuItems(index), MruMenuItem).Filename
            i += 1
            index += 1
        End While

        Return filenames
    End Function

    ' This is used for testing 
    Public Function GetFilesFullEntrystring() As String()
        Dim filenames As String() = New String(m_numEntries - 1) {}

        Dim index As Integer = StartIndex
        Dim i As Integer = 0
        While i < filenames.GetLength(0)
            filenames(i) = MenuItems(index).Text
            i += 1
            index += 1
        End While

        Return filenames
    End Function
#End Region

#Region "Add Methods"

    Public Sub SetFiles(ByVal filenames As String())
        RemoveAll()
        For i As Integer = filenames.GetLength(0) - 1 To 0 Step -1
            AddFile(filenames(i))
        Next
    End Sub

    Public Sub AddFiles(ByVal filenames As String())
        For i As Integer = filenames.GetLength(0) - 1 To 0 Step -1
            AddFile(filenames(i))
        Next
    End Sub

    Public Sub AddFile(ByVal filename As String)
        Dim pathname As String = Path.GetFullPath(filename)
        AddFile(pathname, ShortenPathname(pathname, MaxShortenPathLength))
    End Sub

    ''' <summary>
    ''' Added by LCW--don't want full paths for table and project names
    ''' </summary>
    ''' <param name="entryname">Name of project or table</param>
    Public Sub AddEntry(ByVal entryname As String)
        AddFile(entryname, entryname)
    End Sub

    Public Sub AddFile(ByVal filename As String, ByVal entryname As String)
        If filename Is Nothing Then
            Throw New ArgumentNullException("filename")
        End If

        If filename.Length = 0 Then
            Throw New ArgumentException("filename")
        End If

        If m_numEntries > 0 Then
            Dim index As Integer = FindFilenameMenuIndex(filename)
            If index >= 0 Then
                SetFirstFile(index - StartIndex)
                Return
            End If
        End If

        If m_numEntries < m_maxEntries Then
            Dim menuItem As New MruMenuItem(filename, FixupEntryname(0, entryname), New System.EventHandler(AddressOf OnClick))
            MenuItems.Insert(StartIndex, menuItem)
            SetFirstFile(menuItem)
            Enable()

            If System.Math.Max(System.Threading.Interlocked.Increment(m_numEntries), m_numEntries - 1) = 0 Then
                Enable()
            Else
                FixupPrefixes(1)
            End If
        ElseIf m_numEntries > 1 Then
            Dim menuItem As MruMenuItem = DirectCast(MenuItems(StartIndex + m_numEntries - 1), MruMenuItem)
            MenuItems.RemoveAt(StartIndex + m_numEntries - 1)

            menuItem.Text = FixupEntryname(0, entryname)
            menuItem.Filename = filename

            MenuItems.Insert(StartIndex, menuItem)

            SetFirstFile(menuItem)
            FixupPrefixes(1)
        End If
    End Sub

#End Region

#Region "Remove Methods"

    Public Sub RemoveFile(ByVal number As Integer)
        If number >= 0 AndAlso number < m_numEntries Then
            If System.Threading.Interlocked.Decrement(m_numEntries) = 0 Then
                Disable()
            Else
                Dim startIndex As Integer = startIndex
                If number = 0 Then
                    SetFirstFile(DirectCast(MenuItems(startIndex + 1), MruMenuItem))
                End If

                MenuItems.RemoveAt(startIndex + number)

                If number < m_numEntries Then
                    FixupPrefixes(number)
                End If
            End If
        End If
    End Sub

    Public Sub RemoveFile(ByVal filename As String)
        If m_numEntries > 0 Then
            RemoveFile(FindFilenameNumber(filename))
        End If
    End Sub

    Public Sub RemoveAll()
        If m_numEntries > 0 Then
            For index As Integer = EndIndex - 1 To StartIndex + 1 Step -1
                MenuItems.RemoveAt(index)
            Next
            Disable()
            m_numEntries = 0
            'lcw
            MenuItems.Clear()
        End If
    End Sub

#End Region

#Region "Rename Methods"

    Public Sub RenameFile(ByVal oldFilename As String, ByVal newFilename As String)
        Dim newPathname As String = Path.GetFullPath(newFilename)

        RenameFile(Path.GetFullPath(oldFilename), newPathname, ShortenPathname(newPathname, MaxShortenPathLength))
    End Sub

    Public Sub RenameFile(ByVal oldFilename As String, ByVal newFilename As String, ByVal newEntryname As String)
        If newFilename Is Nothing Then
            Throw New ArgumentNullException("newFilename")
        End If

        If newFilename.Length = 0 Then
            Throw New ArgumentException("newFilename")
        End If

        If m_numEntries > 0 Then
            Dim index As Integer = FindFilenameMenuIndex(oldFilename)
            If index >= 0 Then
                Dim menuItem As MruMenuItem = DirectCast(MenuItems(index), MruMenuItem)
                menuItem.Text = FixupEntryname(0, newEntryname)
                menuItem.Filename = newFilename
                Return
            End If
        End If

        AddFile(newFilename, newEntryname)
    End Sub

#End Region

#Region "Registry Methods"

    Public Property RegistryKeyName() As String
        Get
            Return m_registryKeyName
        End Get
        Set(ByVal value As String)
            If mruStripMutex IsNot Nothing Then
                mruStripMutex.Close()
            End If

            m_registryKeyName = value.Trim()
            If m_registryKeyName.Length = 0 Then
                m_registryKeyName = Nothing
                mruStripMutex = Nothing
            Else
                Dim mutexName As String = m_registryKeyName.Replace("\"c, "_"c).Replace("/"c, "_"c) & "Mutex"
                mruStripMutex = New Mutex(False, mutexName)
            End If
        End Set
    End Property

    Public Sub LoadFromRegistry(ByVal keyName As String)
        RegistryKeyName = keyName
        LoadFromRegistry()
    End Sub

    Public Sub LoadFromRegistry()
        If m_registryKeyName IsNot Nothing Then
            mruStripMutex.WaitOne()

            RemoveAll()

            Dim regKey As RegistryKey = Registry.CurrentUser.OpenSubKey(m_registryKeyName)
            If regKey IsNot Nothing Then
                'm_maxEntries = CInt(regKey.GetValue("max", m_maxEntries))
                Dim num As Integer = CInt(regKey.GetValue("max", m_maxEntries))
                'For number As Integer = m_numEntries To 1 Step -1
                For number As Integer = num To 1 Step -1

                    Dim filename As String = DirectCast(regKey.GetValue("File" & number.ToString()), String)
                    If filename IsNot Nothing Then
                        'LCW change
                        'AddFile(filename)
                        AddEntry(filename)
                    End If
                Next

                regKey.Close()
            End If
            mruStripMutex.ReleaseMutex()
        End If
    End Sub

    Public Sub SaveToRegistry(ByVal keyName As String)
        RegistryKeyName = keyName
        SaveToRegistry()
    End Sub

    Public Sub SaveToRegistry()
        If m_registryKeyName IsNot Nothing Then
            mruStripMutex.WaitOne()

            Dim regKey As RegistryKey = Registry.CurrentUser.CreateSubKey(m_registryKeyName)
            If regKey IsNot Nothing Then
                regKey.SetValue("max", m_numEntries)

                Dim number As Integer = 1
                Dim i As Integer = StartIndex
                While i < EndIndex
                    regKey.SetValue("File" & number.ToString(), DirectCast(MenuItems(i), MruMenuItem).Filename)
                    i += 1
                    number += 1
                End While

                While number <= 16
                    regKey.DeleteValue("File" & number.ToString(), False)
                    number += 1
                End While

                regKey.Close()
            End If
            mruStripMutex.ReleaseMutex()
        End If
    End Sub

    ''' <summary>
    ''' Added by LCW to clear registry entries
    ''' </summary>
    <System.Diagnostics.DebuggerStepThrough()> _
    Public Sub ClearRegistry()
        Try
            With Registry.CurrentUser
                .DeleteSubKeyTree(RegistryKeyName)
            End With
        Catch ex As Exception
        End Try
    End Sub

#End Region
End Class

''' <summary> 
''' Represents an inline most recently used (mru) menu. 
''' </summary> 
''' <remarks> 
''' This class shows the MRU list "inline". To display 
''' the MRU list as a popup menu use <see name="MruMenu">MruMenu</see>. 
''' </remarks> 
Public Class MruStripMenuInline
    Inherits clsMruStripMenu
    Protected owningMenu As ToolStripMenuItem
    Protected firstMenuItem As ToolStripMenuItem

#Region "Construction"

    'private MruStripMenuInline( 

    Public Sub New(ByVal owningMenu As ToolStripMenuItem, ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler)
        Me.New(owningMenu, recentFileMenuItem, clickedHandler, Nothing, False, 4)
    End Sub

    Public Sub New(ByVal owningMenu As ToolStripMenuItem, ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal maxEntries As Integer)
        Me.New(owningMenu, recentFileMenuItem, clickedHandler, Nothing, False, maxEntries)
    End Sub

    Public Sub New(ByVal owningMenu As ToolStripMenuItem, ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal registryKeyName As String)
        Me.New(owningMenu, recentFileMenuItem, clickedHandler, registryKeyName, True, 4)
    End Sub

    Public Sub New(ByVal owningMenu As ToolStripMenuItem, ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal registryKeyName As String, ByVal maxEntries As Integer)
        Me.New(owningMenu, recentFileMenuItem, clickedHandler, registryKeyName, True, maxEntries)
    End Sub

    Public Sub New(ByVal owningMenu As ToolStripMenuItem, ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal registryKeyName As String, ByVal loadFromRegistry As Boolean)
        Me.New(owningMenu, recentFileMenuItem, clickedHandler, registryKeyName, loadFromRegistry, 4)
    End Sub

    Public Sub New(ByVal owningMenu As ToolStripMenuItem, ByVal recentFileMenuItem As ToolStripMenuItem, ByVal clickedHandler As ClickedHandler, ByVal registryKeyName As String, ByVal loadFromRegistry As Boolean, ByVal maxEntries As Integer)
        MaxShortenPathLength = 48
        Me.owningMenu = owningMenu
        Me.firstMenuItem = recentFileMenuItem
        Init(recentFileMenuItem, clickedHandler, registryKeyName, loadFromRegistry, maxEntries)
    End Sub

#End Region

#Region "Overridden Properties"

    Public Overloads Overrides ReadOnly Property MenuItems() As ToolStripItemCollection
        Get
            Return owningMenu.DropDownItems
        End Get
    End Property

    Public Overloads Overrides ReadOnly Property StartIndex() As Integer
        Get
            Return MenuItems.IndexOf(firstMenuItem)
        End Get
    End Property

    Public Overloads Overrides ReadOnly Property EndIndex() As Integer
        Get
            Return StartIndex + NumEntries
        End Get
    End Property

    Public Overloads Overrides ReadOnly Property IsInline() As Boolean
        Get
            Return True
        End Get
    End Property

#End Region

#Region "Overridden Methods"

    Protected Overloads Overrides Sub Enable()
        MenuItems.Remove(recentFileMenuItem)
    End Sub

    Protected Overloads Overrides Sub SetFirstFile(ByVal menuItem As MruMenuItem)
        firstMenuItem = menuItem
    End Sub

    Protected Overloads Overrides Sub Disable()
        Dim index As Integer = MenuItems.IndexOf(firstMenuItem)
        MenuItems.RemoveAt(index)
        MenuItems.Insert(index, recentFileMenuItem)
        firstMenuItem = recentFileMenuItem
    End Sub
#End Region
End Class
